//
//  GQGeDanInfoViewController.m
//  TingMusic
//
//  Created by sunny on 16/5/9.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQGeDanInfoViewController.h"
#import "GQHttpRequest.h"
#import "MidleView.h"
#import <MJExtension/MJExtension.h>
#import "GQExternItem.h"
#import <UIImageView+WebCache.h>
#import "GQGeDanCell.h"
#import "GQMiddleViewItem.h"
#import "GQGeDanItem.h"
@interface GQGeDanInfoViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;
@property (weak, nonatomic) IBOutlet UIView *coverView;
@property (weak, nonatomic) IBOutlet UILabel *tableViewHeaderLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) MidleView *middleView;
@property (weak, nonatomic) IBOutlet UILabel *topHeaderLabel;
@property (strong, nonatomic) NSArray *itemArray;
@property (nonatomic, strong) GQExternItem *externItem;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageViewTop;

@property (nonatomic, assign) CGFloat topConst;
@end

@implementation GQGeDanInfoViewController

- (NSArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    隐藏导航条
    self.navigationController.navigationBarHidden = YES;
    CGFloat top = self.topImageView.gq_height + self.bottomView.gq_height - 30;
    self.topConst = top;
    self.tableView.contentInset = UIEdgeInsetsMake(top , 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, 0, 0, 0);
    
//    添加middleView
    [self addMiddleView];
    
//    加载数据
    [self loadData];
   
}

#pragma mark -添加middleView
- (void)addMiddleView
{
    MidleView *middleView = [MidleView midldleView];
    middleView.frame = self.coverView.frame;
    self.middleView = middleView;
    [self.view addSubview:middleView];
    
}

#pragma mark -加载数据
- (void)loadData
{
    [GQHttpRequest GET:[NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.3.0&channel=xiaomi&operator=2&method=baidu.ting.diy.gedanInfo&format=json&listid=%zd",self.listid] parameters:nil progress:nil success:^(NSURLSessionDataTask *task, NSDictionary * responseObject) {
        
        self.externItem = [GQExternItem mj_objectWithKeyValues:responseObject];
        self.tableViewHeaderLabel.text = self.externItem.desc;
        [self.topImageView sd_setImageWithURL:[NSURL URLWithString:self.externItem.pic_500] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            CGFloat height = self.externItem.height * self.topImageView.gq_width / self.externItem.width;
            UIGraphicsBeginImageContext(self.topImageView.frame.size);
            [image drawInRect:CGRectMake(0, 0, self.topImageView.gq_width, height)];
            self.topImageView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
        }];
        
        self.itemArray = [GQGeDanItem mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        
        self.totalCountLabel.text = [NSString stringWithFormat:@"共%zd首歌",self.itemArray.count];
        
//        给middleView设置数据
        GQMiddleViewItem *item = [GQMiddleViewItem mj_objectWithKeyValues:responseObject];
        self.middleView.middleViewItem = item;
        
//       刷新数据
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error = %@",error);
    }];
}

//点击返回按钮
- (IBAction)backButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"geDanCell";
    GQGeDanCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"GQGeDanCell" owner:nil options:nil] lastObject];
    }
    
    GQGeDanItem *item = self.itemArray[indexPath.row];
    cell.geDanItem = item;
    
    return  cell;
}

@end
